//
//  UIApplication+CurrentVC.m
//  AllToolsOC
//
//  Created by safiri on 2017/6/30.
//  Copyright © 2017年 safiri. All rights reserved.
//

#import "UIApplication+CurrentVC.h"

@implementation UIApplication (CurrentVC)

- (UIViewController*) topMostController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    //  Getting topMost ViewController
    while ([topController presentedViewController])	topController = [topController presentedViewController];
    //  Returning topMost ViewController
    return topController;
}
//弃用 修改参考currentVisibleController
- (UIViewController*)getCurrentViewConttoller{
    UIViewController *currentViewController = [self topMostController];
    if ([currentViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarController = (UITabBarController *)currentViewController;
        return tabBarController.selectedViewController;
    }
    while ([currentViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)currentViewController topViewController])
        currentViewController = [(UINavigationController*)currentViewController topViewController];
    return currentViewController;
}
#pragma mark - Setter & Getter

- (UIWindow *)currentVisibleWindow
{
    NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator];
    for (UIWindow *window in frontToBackWindows){
        BOOL windowOnMainScreen = window.screen == UIScreen.mainScreen;
        BOOL windowIsVisible = !window.hidden && window.alpha > 0;
        BOOL windowLevelNormal = window.windowLevel == UIWindowLevelNormal;
        if (windowOnMainScreen && windowIsVisible && windowLevelNormal) {
            return window;
        }
    }
    return [[[UIApplication sharedApplication] delegate] window];
}

- (UIViewController *)currentVisibleController
{
    UIViewController *topController = self.currentVisibleWindow.rootViewController;
    if (topController && [topController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabbar = (UITabBarController *)topController;
        if (tabbar && tabbar.selectedViewController) {
            if ([tabbar.selectedViewController isKindOfClass:[UINavigationController class]]) {
                UINavigationController *nav = (UINavigationController *)tabbar.selectedViewController;
                UIViewController *vc = nav.visibleViewController;
                return vc;
            }else if ([tabbar.selectedViewController isKindOfClass:[UIViewController class]]) {
                UIViewController *vc = (UIViewController *)tabbar.selectedViewController;
                return vc;
            }
        }
        
    }
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }
    return topController;
}
- (UIViewController *)mainRootViewController {
    return [[[[UIApplication sharedApplication] delegate] window] rootViewController];
}

@end
